{% extends 'generic/object_create.html' %}
{% load form_helpers %}
{% load helpers %}

{% block form %}
    <div class="card">
        <div class="card-header"><strong>Job Source</strong></div>
        <div class="card-body">
            <div class="mb-10 d-lg-flex justify-content-center">
                <label class="col-lg-3 col-form-label">Module</label>
                <div class="col-lg-9 pt-6">
                    <code>{{ obj.module_name }}</code>
                    <span class="form-text">Dotted name of the Python module providing this job</span>
                </div>
            </div>
            <div class="mb-10 d-lg-flex justify-content-center">
                <label class="col-lg-3 col-form-label">Job Class</label>
                <div class="col-lg-9 pt-6">
                    <code>{{ obj.job_class_name }}</code>
                    <span class="form-text">Name of the Python class providing this job</span>
                </div>
            </div>
            <div class="mb-10 d-lg-flex justify-content-center">
                <div class="col-lg-9 offset-lg-3">
                    <div class="form-check mt-6">
                        <input class="form-check-input" id="id_installed" name="installed" type="checkbox" disabled {% if obj.installed %}checked{% endif %}>
                        <label class="form-check-label" for="id_installed">
                            Installed
                            <span class="form-text">
                                Whether the Python class providing this job is presently installed and loadable
                            </span>
                        </label>
                    </div>
                </div>
            </div>
            <div class="mb-10 d-lg-flex justify-content-center">
                <div class="col-lg-9 offset-lg-3">
                    <div class="form-check mt-6">
                        <input class="form-check-input" id="id_supports_dryrun" name="supports_dryrun" type="checkbox" disabled {% if obj.supports_dryrun %}checked{% endif %}>
                        <label class="form-check-label" for="id_supports_dryrun">
                            Supports dryrun
                            <span class="form-text">
                                Whether this job supports bypassing approval when running with dryrun argument set to true
                            </span>
                        </label>
                    </div>
                </div>
            </div>
            <div class="mb-10 d-lg-flex justify-content-center">
                <div class="col-lg-9 offset-lg-3">
                    <div class="form-check mt-6">
                        <input class="form-check-input" id="id_is_job_hook_receiver" name="is_job_hook_receiver" type="checkbox" disabled {% if obj.is_job_hook_receiver %}checked{% endif %}>
                        <label class="form-check-label" for="id_is_job_hook_receiver">
                            Is job hook receiver
                            <span class="form-text">
                                Whether this job is a job hook receiver
                            </span>
                        </label>
                    </div>
                </div>
            </div>
            <div class="mb-10 d-lg-flex justify-content-center">
                <div class="col-lg-9 offset-lg-3">
                    <div class="form-check mt-6">
                        <input class="form-check-input" id="id_is_job_button_receiver" name="is_job_button_receiver" type="checkbox" disabled {% if obj.is_job_button_receiver %}checked{% endif %}>
                        <label class="form-check-label" for="id_is_job_button_receiver">
                            Is job button receiver
                            <span class="form-text">
                                Whether this job is a job button receiver
                            </span>
                        </label>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="card">
        <div class="card-header"><strong>Job</strong></div>
        <div class="card-body">
            {% render_field form.enabled %}
            {% include "extras/inc/overridable_field.html" with field=form.name override_field=form.name_override %}
            {% include "extras/inc/overridable_field.html" with field=form.grouping override_field=form.grouping_override %}
            {% include "extras/inc/overridable_field.html" with field=form.description override_field=form.description_override %}
        </div>
    </div>
    <div class="card">
        <div class="card-header"><strong>Properties</strong></div>
        <div class="card-body">
            {% include "extras/inc/overridable_field.html" with field=form.dryrun_default override_field=form.dryrun_default_override %}
            {% include "extras/inc/overridable_field.html" with field=form.hidden override_field=form.hidden_override %}
            {% include "extras/inc/overridable_field.html" with field=form.soft_time_limit override_field=form.soft_time_limit_override %}
            {% include "extras/inc/overridable_field.html" with field=form.time_limit override_field=form.time_limit_override %}
            {% include "extras/inc/overridable_field.html" with field=form.has_sensitive_variables override_field=form.has_sensitive_variables_override %}
            {% include "extras/inc/overridable_field.html" with field=form.job_queues override_field=form.job_queues_override %}
            {% include "extras/inc/overridable_field.html" with field=form.default_job_queue override_field=form.default_job_queue_override %}
            {% include "extras/inc/overridable_field.html" with field=form.is_singleton override_field=form.is_singleton_override %}
        </div>
    </div>
    {% include 'inc/extras_features_edit_form_fields.html' %}
{% endblock form %}

{% block javascript %}
    {{ block.super }}
    {{ obj.job_class.properties_dict | json_script:"job_class_properties" }}
    <script>
        /*
         * Because some of the form fields are Select2 widgets, we need to stick with jQuery here for now
         */
        const job_class_properties = JSON.parse(document.getElementById("job_class_properties").textContent);
        const description_default = $("#id_description").val();
        /* All id_*_override fields */
        $("[id^=id_][id$=_override]").change(function() {
            const override_id = $(this).attr("id");
            /* What's the form field this is an override for? */
            const field_id = override_id.slice(0, -9);
            const field = $("#" + field_id);
            const field_name = field_id.slice(3);
            if ($(this).prop("checked")) {
                field.attr("readonly", null);
                field.prop("disabled", false);
            } else {
                /* Revert the field to its default value */
                if (field.attr("type") == "checkbox") {
                    /* Do we have a job class object to derive properties from? */
                    if (job_class_properties != "") {
                        field.prop("checked", job_class_properties[field_name]);
                    } else {
                        /* No, we don't - it's probably not installed */
                        field.prop("checked", field.attr("checked"));
                    }
                    /*
                     * Checkboxes can't be "readonly" in HTML :-(
                     * Note that "disabled" fields aren't included in form submission, so we have to be sure to
                     * re-enable them at form submission time.
                     */
                    field.prop("disabled", true);
                } else {
                    /* Do we have a job class object to derive properties from? */
                    if (job_class_properties != "") {
                        if (field_name != "job_queues" && field_name != "default_job_queue") {
                            field.val(job_class_properties[field_name]);
                        }
                    } else if (field_name != "description") {
                        /* No, we don't - it's probably not installed */
                        field.val(field.attr("value"));
                    } else {
                        /* Textarea fields don't have a "value" attribute */
                        field.val(description_default);
                    }
                    if (field_name == "job_queues" || field_name == "default_job_queue") {
                        field.prop("disabled", true);
                    } else {
                        field.attr("readonly", "readonly");
                    }
                }
            }
        });
        $("[id^=id_][id$=_override]").change();

        /*
         * As mentioned above, we need to re-enable all checkboxes, job_queues and default_job_queue fields
         * on form submission so they get included
         */
        $("form").submit(function() {
            $('input[type="checkbox"]:disabled').each(function() {
                $(this).prop("disabled", false);
            });
            $('#id_job_queues').prop("disabled", false);
            $('#id_default_job_queues').prop("disabled", false);
        });
    </script>
{% endblock javascript %}
